/*	This program produces Table C2 */

***** Set directories 
local dir_clean 	"~/Dropbox/Retirement gaming/clean"
local dir_output 	"~/Dropbox/Retirement gaming/output/dataverse"


use "`dir_clean'/mainsample_medbcw.dta", clear

* Sets of controls		
local listcontrols1 = " i.t  " 
local listcontrols2 = " i.t i.ciiu2_1stobs i.ndep_cat_1stobs i.ndep_cat " 
local listcontrols3 = " i.t i.ciiu2_1stobs i.ndep_cat_1stobs i.ndep_cat i.year#i.i.ciiu2_1stobs i.year#i.ndep_cat_1stobs "  

*Transition cohorts 
local listcontrols1transition = " i.t i.t#transition "
local listcontrols2transition = " i.t i.t#transition i.ndep_cat_1stobs i.ndep_cat_1stobs#i.transition i.ciiu2_1stobs i.ciiu2_1stobs#i.transition i.ndep_cat i.ndep_cat#i.transition "
local listcontrols3transition = " i.t i.t#transition i.ndep_cat_1stobs i.ndep_cat_1stobs#i.transition i.ciiu2_1stobs i.ciiu2_1stobs#i.transition i.ndep_cat i.ndep_cat#i.transition i.year#i.ciiu2_1stobs i.year#i.ciiu2_1stobs#i.transition i.year#i.ndep_cat_1stobs i.year#i.ndep_cat_1stobs#i.transition "

*Employees
local listcontrols1withempl = " i.t i.t#withempl "
local listcontrols2withempl = " i.t i.t#withempl i.ndep_cat_1stobs i.ndep_cat_1stobs#i.withempl i.ciiu2_1stobs i.ciiu2_1stobs#i.withempl i.ndep_cat i.ndep_cat#i.withempl "
local listcontrols3withempl = " i.t i.t#withempl i.ndep_cat_1stobs i.ndep_cat_1stobs#i.withempl i.ciiu2_1stobs i.ciiu2_1stobs#i.withempl i.ndep_cat i.ndep_cat#i.withempl i.year#i.ciiu2_1stobs i.year#i.ciiu2_1stobs#i.withempl i.year#i.ciiu2_1stobs i.year#i.ciiu2_1stobs#i.withempl"


* Reporting 70%
estimates clear
foreach c in 1 2 3 {
*Keep those observed at least 70% of the time
		preserve
		keep if  self_empl==1 & prop_mself_empl>=.7 
		*Normalize wages
		sum W if time_bcw==-1
		local meanw=r(mean)
		g Wz=W/`meanw'		
		* Create sample
		reghdfe Wz pre6 pre25 post03 post4, absorb(`listcontrols3') // semi-parametric
		g thissample=e(sample)
		* Regress
		eststo: reghdfe Wz pre6 pre25 post03 post4 if thissample==1, absorb(`listcontrols`c'') vce(cluster i ) 
		restore
}
* Transition
foreach c in 1 2 3 {
	*Keep those observed at least X% of the time
		preserve
		keep if  self_empl==1 
		*Normalize wages
		sum W if time_bcw==-1 & transition==0
		local meanw=r(mean)
		g Wz=W/`meanw'
		sum W if time_bcw==-1 & transition==1
		local meanw=r(mean)
		replace Wz=W/`meanw' if transition==1
		* Regress
		reghdfe Wz pre6 pre25 post03 post4 pre6_transition pre25_transition post03_transition post4_transition , absorb(`listcontrols3transition') // semi-parametric
		g thissample=e(sample)
		eststo: reghdfe Wz pre6 pre25 post03 post4 pre6_transition pre25_transition post03_transition post4_transition  if thissample==1, absorb(`listcontrols`c'transition') vce(cluster i )
		restore
}
* With employees
 foreach c in 1 2 3 {
	*Keep those observed at least X% of the time
		preserve
		keep if  self_empl==1 
		*Normalize wages
		sum W if time_bcw==-1 & withempl==0
		local meanw=r(mean)
		g Wz=W/`meanw'
		sum W if time_bcw==-1 & withempl==1
		local meanw=r(mean)
		replace Wz=W/`meanw' if withempl==1
		* Regress
		reghdfe Wz pre6 pre25 post03 post4 pre6_withempl pre25_withempl post03_withempl post4_withempl , absorb(`listcontrols3withempl') // semi-parametric
		g thissample=e(sample)
		eststo: reghdfe Wz pre6 pre25 post03 post4 pre6_withempl pre25_withempl post03_withempl post4_withempl if thissample==1, absorb(`listcontrols`c'withempl') vce(cluster i )
		restore
}
* Table with results 
esttab using "`dir_output'/tableC2.tex", ///
	keep(pre25* post03* post4*)  stats(N N_clust1, label("Observations" "Individuals") fmt(%9.0gc)) ///
	b(%4.3f) se(%4.3f) star(* 0.10 ** 0.05 *** 0.01) ///
	f nomtitles  nogaps replace label nonumbers nolines prefoot(\hline) 


clear all
exit
